//
//  PersonGrowViewController.m
//  XRSquare
//
//  Created by 穆相臣 on 2018/8/24.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "PersonGrowViewController.h"
#import "GrowContentTableViewCell.h"
#import "GrowContentNoeditTableViewCell.h"
#import <UMShare/UMShare.h>

@interface PersonGrowViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * contentTableView;
    NSArray * dataArr;
    UIScrollView * tagsScrollView;
    UIButton * button1;
    UIButton * button2;
    UIButton * button3;
    UIButton * button4;
    UIButton * button5;
    UIButton * button6;
    UIView * selectorView;
    
}

@end

@implementation PersonGrowViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        self.contentTargetselector = @"person";
        self.type = 0;
    }
    return self;
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    if ([self.contentTargetselector isEqualToString:@"person"])
    {
        self.navigationItem.title = @"成长档案";
    }
    else if ([self.contentTargetselector isEqualToString:@"person_select"])
    {
        self.navigationItem.title = @"个人档案";
    }
    else
    {
        self.navigationItem.title = @"成长历程";
    }

    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
    
    CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];
    CGRect navRect = self.navigationController.navigationBar.frame;
    
    tagsScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, statusRect.size.height + navRect.size.height, ScreenWidth, 50)];
    tagsScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:tagsScrollView];
    tagsScrollView.contentSize = CGSizeMake(90 * 6, 50);
    
    button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 90, 50)];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    button1.titleLabel.font = [UIFont systemFontOfSize:15];
    button1.tag = 1;
    [button1 setTitle:@"个人大事记" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(selectionAction:) forControlEvents:UIControlEventTouchUpInside];
    [tagsScrollView addSubview:button1];
    button1.selected = YES;

    button2 = [[UIButton alloc]initWithFrame:CGRectMake(90, 0, 90, 50)];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    button2.titleLabel.font = [UIFont systemFontOfSize:15];
    button2.tag = 2;
    [button2 setTitle:@"个人采风" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(selectionAction:) forControlEvents:UIControlEventTouchUpInside];
    [tagsScrollView addSubview:button2];
    
    button3 = [[UIButton alloc]initWithFrame:CGRectMake(180, 0, 90, 50)];
    [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    button3.titleLabel.font = [UIFont systemFontOfSize:15];
    button3.tag = 3;
    [button3 setTitle:@"校园活动" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(selectionAction:) forControlEvents:UIControlEventTouchUpInside];
    [tagsScrollView addSubview:button3];
    
    button4 = [[UIButton alloc]initWithFrame:CGRectMake(270, 0, 90, 50)];
    [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button4 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    button4.titleLabel.font = [UIFont systemFontOfSize:15];
    button4.tag = 4;
    [button4 setTitle:@"社会活动" forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(selectionAction:) forControlEvents:UIControlEventTouchUpInside];
    [tagsScrollView addSubview:button4];
    
    button5 = [[UIButton alloc]initWithFrame:CGRectMake(360, 0, 90, 50)];
    [button5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button5 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    button5.titleLabel.font = [UIFont systemFontOfSize:15];
    button5.tag = 5;
    [button5 setTitle:@"技能资质" forState:UIControlStateNormal];
    [button5 addTarget:self action:@selector(selectionAction:) forControlEvents:UIControlEventTouchUpInside];
    [tagsScrollView addSubview:button5];
    
    button6 = [[UIButton alloc]initWithFrame:CGRectMake(450, 0, 90, 50)];
    [button6 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button6 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    button6.titleLabel.font = [UIFont systemFontOfSize:15];
    button6.tag = 6;
    [button6 setTitle:@"梦想舞台" forState:UIControlStateNormal];
    [button6 addTarget:self action:@selector(selectionAction:) forControlEvents:UIControlEventTouchUpInside];
    [tagsScrollView addSubview:button6];
    
    selectorView = [[UIView alloc]initWithFrame:CGRectMake(10, 45, 70, 5)];
    selectorView.backgroundColor = [UIColor orangeColor];
    [tagsScrollView addSubview:selectorView];
    
    if ([self.contentTargetselector isEqualToString:@"person"])
    {
        contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, statusRect.size.height + navRect.size.height, ScreenWidth, ScreenHeight - statusRect.size.height - navRect.size.height) style:UITableViewStylePlain];
    }
    else if ([self.contentTargetselector isEqualToString:@"person_select"])
    {
        contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, statusRect.size.height + navRect.size.height + 50, ScreenWidth, ScreenHeight - 50 - statusRect.size.height - navRect.size.height) style:UITableViewStylePlain];
    }
    else
    {
        contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, statusRect.size.height + navRect.size.height, ScreenWidth, ScreenHeight - statusRect.size.height - navRect.size.height) style:UITableViewStylePlain];
    }

 
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [self.view addSubview:contentTableView];
    [self requestData];
}

-(void)selectionAction:(UIButton *)button
{
    button1.selected = NO;
    button2.selected = NO;
    button3.selected = NO;
    button4.selected = NO;
    button5.selected = NO;
    button6.selected = NO;

    button.selected = !button.selected;
    
    self.type = [NSString stringWithFormat:@"%ld",button.tag - 1];
    selectorView.frame = CGRectMake(10 + 90 * (button.tag - 1), 45, 70, 5);
    [self requestData];
}

-(void)requestData
{
    if ([self.contentTargetselector isEqualToString:@"person"])
    {
        [NetRequestCenter loadpersonalarchiveWityType:@"6" property:@"4" ordrebase:@"0" orderrule:@"0" page:@"1" time:@"" nexttime:@"" showtype:@"3" uid:self.contentId Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
        
    }
    else if ([self.contentTargetselector isEqualToString:@"person_select"])
    {
        [NetRequestCenter loadpersonalarchiveWityType:self.type property:@"4" ordrebase:@"0" orderrule:@"0" page:@"1" time:@"" nexttime:@"" showtype:@"3" uid:self.contentId Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
        
    }
    else
    {
        [NetRequestCenter companyarchivejsonwithuid:self.contentId page:@"1" orderrule:@"0" orderbase:@"0" type:@"6" result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArr count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [GrowContentNoeditTableViewCell heightwithData:[dataArr objectAtIndex:indexPath.row]];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifer = @"";
    GrowContentNoeditTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell = [[GrowContentNoeditTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    [cell loadData:[dataArr objectAtIndex:indexPath.row]];
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
}

-(void)showImages:(NSArray *)images
{
    MYPhotoBrowser *photoBrowser = [[MYPhotoBrowser alloc]initWithUrls:images imgViews:nil placeholder:nil currentIdx:index handleNames:nil callback:^(UIImage *handleImage,NSString *handleType) {
        
    }];
    [photoBrowser showWithAnimation:YES];
}

-(void)shareAction
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * item1 = [UIAlertAction actionWithTitle:@"QQ" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self qqAction];
    }];
    UIAlertAction * item2 = [UIAlertAction actionWithTitle:@"微信" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self wechatFriendAction];
    }];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alertController addAction:item1];
    [alertController addAction:item2];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)wechatFriendAction
{
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建网页内容对象
    NSString * description = [NSString stringWithFormat:@"%@——成长历程",self.companyName];
    NSString * imageUrl = self.logoUrl;
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"蓄锐广场" descr:description thumImage:imageUrl];
    //设置网页地址
    shareObject.webpageUrl = [NSString stringWithFormat:@"https://app.xrsquare.com/v180615/share/develop?uid=%@",self.contentId];
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}

-(void)qqAction
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建网页内容对象
    NSString * description = [NSString stringWithFormat:@"%@——成长历程",self.companyName];
    NSString * imageUrl = self.logoUrl;
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"蓄锐广场" descr:description thumImage:imageUrl];    //设置网页地址
    shareObject.webpageUrl = [NSString stringWithFormat:@"https://app.xrsquare.com/v180615/share/develop?uid=%@",self.contentId];
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
